//
//  CityViewController.m
//  chanyou
//
//  Created by wanli on 14/12/31.
//  Copyright (c) 2014年 wanli. All rights reserved.
//---------------XX口袋书----------------

#import "CityViewController.h"
#import "CityTableViewCell.h"
#import "ConnectModel.h"
#import "UIImageView+WebCache.h"
#import "DissertationViewController.h"
#import "JourneyViewController.h"
#import "TravelViewController.h"
#import "NotesViewController.h"

#import "SDImageCache.h"

@interface CityViewController ()
@end

@implementation CityViewController
- (void)dealloc
{
    [_cityArray release];
    [_tableView release];
    [_destination_id release];
    [_ID release];
    [_reloadImg release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.cityArray = [[NSMutableArray alloc]init];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self startConnect];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 45, 100)];
    label.textColor = [UIColor colorWithRed:255 / 255.0 green:69 / 255.0 blue:0 alpha:1];
    label.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:20];
    label.text = @"目的地";
    self.navigationItem.titleView = label;
    [label release];

    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
//    self.view.backgroundColor = [UIColor grayColor];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
//    self.tableView = [[[UITableView alloc] init]autorelease];
//
    [self.view addSubview:_tableView];
    [_tableView release];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[CityTableViewCell class] forCellReuseIdentifier:@"cityCell"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   
    // 风火轮
    self.reloadImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"reload.png"]];
    _reloadImg.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2 - 64);
    // _reloadImg.clipsToBounds = YES;
    [self.view addSubview:_reloadImg];
    [_reloadImg release];
    
    CABasicAnimation *rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    rotationAnimation.duration = 2;
    rotationAnimation.repeatCount = 10000;
    rotationAnimation.cumulative = NO;
    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;
    [_reloadImg.layer addAnimation:rotationAnimation forKey:@"rotation"];
    
}

- (void)viewWillAppear:(BOOL)animated{


}
- (void)viewDidAppear:(BOOL)animated{
//    [self startConnect];
}
- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
    //    [[SDImageCache sharedImageCache] clearMemory];
}

- (void)startConnect{
    __block CityViewController *city = self;
    NSString *path = [NSString stringWithFormat:@"%@%@.json", @"http://chanyouji.com/api/destinations/", _ID];
    [ConnectModel connectModelWithUrl:path parmaters:nil finished:^(NSData *data) {
        
        [city.reloadImg removeFromSuperview];
        
        city.cityArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [city.tableView reloadData];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_cityArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cityCell"];
    NSDictionary *dic = [_cityArray objectAtIndex:indexPath.row];
    NSString *path = [dic objectForKey:@"image_url"];
//    [cell.view imageWithUrl:path placeHolder:nil];
    [cell.view sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
    cell.name.text = [NSString stringWithFormat:@"%@ %@", [dic objectForKey:@"name_zh_cn"], [dic objectForKey:@"name_en"]];
  
    [cell.dissertation addTarget:self action:@selector(dissertationButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [cell.journey addTarget:self action:@selector(journeyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.travel addTarget:self action:@selector(travelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.notes addTarget:self action:@selector(notesButtonAction:) forControlEvents:UIControlEventTouchUpInside];
   
    return cell;
}


- (void)dissertationButtonAction:(UIButton *)sender{
    DissertationViewController *dis = [[DissertationViewController alloc] init];
//    NSLog(@"id = %@", _destination_id);
    UIView *v = [sender superview];
    UITableViewCell *cell = (UITableViewCell *)[v superview];
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
//    NSLog(@"section = %ld", index.section);
  
     NSDictionary *dic = [_cityArray objectAtIndex:index.row];
    dis.destination_id = [dic objectForKey:@"id"];
    
    [self.navigationController pushViewController:dis animated:YES];
        [[SDImageCache sharedImageCache] clearMemory];
    [dis release];
}
- (void)journeyButtonAction:(UIButton *)sender{
    JourneyViewController *dis = [[JourneyViewController alloc] init];
//    NSLog(@"id = %@", _destination_id);
    UIView *v = [sender superview];
    UITableViewCell *cell = (UITableViewCell *)[v superview];
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    //    NSLog(@"%ld", index.row);
    NSDictionary *dic = [_cityArray objectAtIndex:index.row];
    dis.destination_id = [dic objectForKey:@"id"];
    
    [self.navigationController pushViewController:dis animated:YES];
        [[SDImageCache sharedImageCache] clearMemory];
    [dis release];
}
- (void)travelButtonAction:(UIButton *)sender{
    TravelViewController *dis = [[TravelViewController alloc] init];
//    NSLog(@"id = %@", _destination_id);
    UIView *v = [sender superview];
    UITableViewCell *cell = (UITableViewCell *)[v superview];
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    //    NSLog(@"%ld", index.row);
    NSDictionary *dic = [_cityArray objectAtIndex:index.row];
    dis.destination_id = [dic objectForKey:@"id"];
    
//    [self presentViewController:dis animated:YES completion:nil];
    [self.navigationController pushViewController:dis animated:YES];
        [[SDImageCache sharedImageCache] clearMemory];
    [dis release];
}
- (void)notesButtonAction:(UIButton *)sender{
    NotesViewController *dis = [[NotesViewController alloc] init];
//    NSLog(@"id = %@", _destination_id);
    UIView *v = [sender superview];
    UITableViewCell *cell = (UITableViewCell *)[v superview];
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    //    NSLog(@"%ld", index.row);
    NSDictionary *dic = [_cityArray objectAtIndex:index.row];
    dis.destination_id = [dic objectForKey:@"id"];
    
    [self.navigationController pushViewController:dis animated:YES];
    [[SDImageCache sharedImageCache] clearMemory];
    [dis release];
}


//- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated{
//    scrollPosition = UITableViewScrollPositionBottom;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.frame.size.height / 2 + 10;
}














- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
